package com.itheima.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.AdChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author danzhiheng
 * @version 1.1
 * @date 2023/1/30 17:08
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    /**
     * 频道列表分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo findPage(AdChannelPageRequestDto dto) {
        //1. 创建分页对象
        IPage<AdChannel> pageInfo = new Page(dto.getPage(),dto.getSize());
        //2. 创建条件对象
        LambdaQueryWrapper<AdChannel> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isEmpty(dto.getName()),AdChannel::getName,dto.getName());
        lqw.eq(dto.getStatus() != null,AdChannel::getStatus,dto.getStatus());
        //3. 执行查询
        IPage<AdChannel> pageResult = page(pageInfo, lqw);
        System.out.println("pageResult==pageInfo:::" + (pageResult==pageInfo));
        //4. 封装对象
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),pageInfo.getTotal(),pageInfo.getRecords());
    }

    /**
     * 添加频道
     *
     * @param adChannel
     */
    @Override
    public void add(AdChannel adChannel) {
        adChannel.setCreatedTime(new Date());
        adChannel.setIsDefault(true);
        save(adChannel);
    }
}
